// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: WIZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley/Tails LP
// ----------------------------------

// Aliases
private alias 40 : TYPE_WIZSETUP

// Function declarations
reserve function WIZSetup_SpeedUpMusic1P
reserve function WIZSetup_SlowDownMusic1P
reserve function WIZSetup_SpeedUpMusic2P
reserve function WIZSetup_SlowDownMusic2P

// Static Values
public value WIZSetup_value33 = 0;

private alias WIZSetup_value33 : WIZSetup_AchievementValue

// Tables
public table WIZSetup_table39
	508, 24, 509, 10, 510, 12, 511, 24, 510, 12, 509, 10
end table

public table WIZSetup_table40
	512, 128, 513, 20, 512, 8, 513, 8, 512, 8, 513, 8
end table

public table WIZSetup_table41
	514, 128, 515, 12, 514, 12, 515, 12, 514, 6, 515, 6, 514, 6, 515, 6
end table

public table WIZSetup_table42
	516, 8, 517, 8
end table

public table WIZSetup_table43
	518, 128, 519, 8, 518, 8, 519, 8, 518, 8, 519, 12, 518, 12, 519, 12
	
end table

public table WIZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

private table WIZSetup_demoTable1
	0x440000, 0x2910000, 32, 1, 40, 27, 8, 39, 56, 1, 40, 9, 8, 40, 56, 1, 40, 6, 8, 8, 56, 1, 40, 6, 8, 175, 56, 1, 40, 7, 8, 111, 0, 2, 4, 35, 52, 1, 36, 7, 4, 73, 0, 4, 8, 4, 56, 1, 40, 7, 8, 68, 0, 5, 4, 30, 0, 8, 2, 4, 50, 1, 34, 1, 50, 1, 32, 4, 0, 15, 8, 56, 0, 1, 2, 32, 50, 1, 34, 1, 42, 1, 40, 3, 8, 255, 8, 97, 0, 12, 4, 5, 0, 26, 8, 21, 56, 1, 40, 9, 8, 255, 8, 4, 10, 3, 2, 7, 10, 2
end table

private table WIZSetup_demoTable2
	0x440000, 0x2950000, 32, 13, 40, 38, 8, 47, 56, 1, 40, 12, 8, 224, 56, 1, 40, 5, 8, 69, 56, 1, 40, 8, 32, 2, 0, 2, 8, 12, 0, 28, 8, 16, 0, 1, 8, 255, 8, 5, 56, 1, 40, 24, 8, 218, 56, 1, 40, 9, 8, 50, 0, 1, 4, 7, 0, 16, 8, 174, 56, 1, 40, 12, 8, 7, 56, 1, 40, 8, 8, 4, 56, 1, 40, 5, 8, 3, 4, 2, 52, 1, 36, 5, 4, 4, 52, 1, 32, 5, 0, 4, 4, 1, 52, 1, 36, 3, 4, 4, 52, 1, 36, 4, 4, 5, 52, 1, 36, 3
	4, 1, 0, 2, 8, 2, 56, 1, 40, 3, 32, 2, 0, 1, 8, 5, 0, 5, 8, 3, 56, 1, 40, 2, 8, 1, 0, 9, 8, 5, 0, 15, 8, 22, 0, 3, 4, 2, 52, 1, 36, 9, 32, 2, 40, 8, 8, 4, 56, 1, 40, 4, 32, 1, 0, 3, 48, 1, 32, 6, 0, 4, 48, 1, 32, 5, 0, 1, 8, 3, 0, 1, 48, 1, 32, 5, 0, 3, 48, 1, 32, 3, 40, 3, 8, 2, 0, 1, 48, 1, 32, 5, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 5, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 3, 48, 1, 32, 5, 0, 3, 48, 1, 32, 6, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 40, 2, 8, 39, 56, 1, 40, 12, 8, 13, 0, 17, 8, 4, 0, 12, 4, 11, 8, 8
end table

private table WIZSetup_demoTable3
	0x440000, 0x2910000, 0, 18, 8, 77, 56, 1, 40, 13, 8, 70, 56, 1, 40, 14, 8, 3, 0, 22, 4, 4, 0, 3, 48, 1, 32, 16, 40, 3, 8, 1, 0, 30, 48, 1, 32, 38, 40, 149, 8, 11, 0, 3, 4, 6, 0, 4, 4, 5, 0, 5, 8, 13, 0, 14, 4, 89, 0, 6, 8, 44, 10, 1, 2, 13, 0, 52, 8, 59, 56, 1, 40, 17, 8, 18, 0, 4, 4, 33, 0, 1, 8, 105, 56, 1, 40, 30, 8, 45, 4, 6, 0, 52, 8, 6, 56, 1, 40, 19, 8, 54, 10, 6, 2, 17, 10, 2, 8, 58, 56, 1, 40, 4, 8, 197, 56, 1, 41, 10, 9, 14, 57, 1, 41, 40, 33, 4, 1, 126, 9, 12, 8, 6, 56, 1, 40, 12, 
end table

function WIZSetup_SpeedUpMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 1)
			SwapMusicTrack("SPEEDSHOES.ogg", 0, 0, 1)
			break
		case 2
			SetMusicTrack("SPEEDSHOES.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("SPEEDSHOES.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function WIZSetup_SlowDownMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 1)
			SwapMusicTrack("WISLE.ogg", 0, 001386983, 1)
			break
		case 2
			SetMusicTrack("WISLE.ogg", 0, 001386983)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("WISLE.ogg", 0, 001386983)
			SetMusicTrack("Invincibility.ogg", 2, 1)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
		object.value0++
		if object.value0 > 7
			tileLayer[1].deformationOffset++
			object.value0 = 0
		end if
	if object.state < 5
		object.value1++
		if object.value1 == 8
			object.value1 = 0
			GetPaletteEntry(0, 159, temp1)
			GetPaletteEntry(0, 158, temp0)
			SetPaletteEntry(0, 159, temp0)
			GetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 158, temp0)
			GetPaletteEntry(0, 147, temp0)
			SetPaletteEntry(0, 148, temp0)
			SetPaletteEntry(0, 147, temp1)
		end if
	end if
	if WIZSetup_AchievementValue == false
		if stage.debugMode == false
			if specialStage.emeralds >= 127 // Not 127 emeralds, it's 01111111, or seven ones, in bits
				if options.gameMode == 0
					WIZSetup_AchievementValue = true
					CallNativeFunction2(SetAchievement, 2, 100) // Get all emeralds in Emerald Hill achievment
				end if
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					arrayPos1 += 4
					if saveRAM[arrayPos1] < 20
						WIZSetup_AchievementValue = true
						CallNativeFunction2(SetAchievement, 2, 100)
					end if
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	rankS = 003099
	rankA = 004599
	rankB = 005000
	rankC = 005599

	if options.expertmode == 1
		LoadPalette("WIZ/WIZExpert.act", 0, 0, 0, 255)
	else
		LoadPalette("WIZ/WIZ.act", 0, 0, 0, 255)
	end if
	LoadPalette("WIZ/BW.act", 2, 0, 0, 255)
	SetMusicTrack("WISLE.ogg", 0, 001386983)
	SpeedUpMusic = WIZSetup_SpeedUpMusic1P
	SlowDownMusic = WIZSetup_SlowDownMusic1P
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Ricky]
	//tileLayer[1].scrollPos = -0x80000
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, WIZSetup_table38)
		arrayPos0++
	loop
	Stage[0].ActiveLayer= 1
	Stage[1].ActiveLayer= 2
	object[10].type = TypeName[WIZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[20].type = TypeName[BG Clouds]
	SetPaletteEntry(0, 192, 0)
	if options.attractMode == true
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = WIZSetup_demoTable1
			PlayerObject_ReplaySize = 110
			PlayerObject_ReplayLength = 0x654
			break
		case 1
			PlayerObject_ReplayTable = WIZSetup_demoTable2
			PlayerObject_ReplaySize = 318
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = WIZSetup_demoTable3
			PlayerObject_ReplaySize = 138
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
